package com.queen.testretrofitrxjava.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.queen.testretrofitrxjava.R;
import com.queen.testretrofitrxjava.entity.Subject;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by liukun on 16/3/12.
 */
public class TopMovieAdapter extends RecyclerView.Adapter<TopMovieAdapter.TopMovieViewHolder> {

    private Context context;
    private List<Subject> movieArray;

    public OnItemClickListener mOnItemClickListener;

    @Inject
    public TopMovieAdapter(Context context) {
        this.context = context;
        this.movieArray = new ArrayList<>();
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public TopMovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(context).inflate(R.layout.item_top_movie, parent, false);
        return new TopMovieViewHolder(item);
    }

    @Override
    public void onBindViewHolder(final TopMovieViewHolder holder, final int position) {
//        holder.bindHolder(movieArray.get(position));

        Subject subject = movieArray.get(position);
        holder.movieTitleTV.setText(subject.getTitle());
        holder.directorTV.setText("导演：" + getNames(subject.getDirectors()));
        holder.castTV.setText("演员：" + getNames(subject.getCasts()));
        holder.iv.setImageURI(Uri.parse(subject.getImages().getMedium()));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return movieArray.size();
    }


    /**
     * ViewHolder
     */
    public static class TopMovieViewHolder extends RecyclerView.ViewHolder {

        private View itemView;
        private SimpleDraweeView iv;
        private TextView movieTitleTV;
        private TextView directorTV;
        private TextView castTV;

        public TopMovieViewHolder(View itemView) {
            super(itemView);

            this.itemView = itemView;

            iv = (SimpleDraweeView) itemView.findViewById(R.id.item_top_movie_IV);
            movieTitleTV = (TextView) itemView.findViewById(R.id.item_top_movie_title_TV);
            directorTV = (TextView) itemView.findViewById(R.id.item_top_movie_director_TV);
            castTV = (TextView) itemView.findViewById(R.id.item_top_movie_cast_TV);
        }

    }

    public static String getNames(List<Subject.Cast> castArray){
        StringBuilder sb = new StringBuilder();
        for (Subject.Cast cast : castArray) {
            sb.append(cast.getName() + "/");
        }

        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public void setMovieArray(List<Subject> movieArray){
        this.movieArray = movieArray;
        this.notifyDataSetChanged();
    }
}
